package com.ruoyi.agriculture.guo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.agriculture.guo.domain.AgriConversation;
import com.ruoyi.agriculture.guo.service.IAgriConversationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 农业问答会话Controller
 * 
 * @author ruoyi
 * @date 2025-07-31
 */
@RestController
@RequestMapping("/agriculture/guo/conversation")
public class AgriConversationController extends BaseController
{
    @Autowired
    private IAgriConversationService agriConversationService;

    /**
     * 查询农业问答会话列表
     */
    @PreAuthorize("@ss.hasPermi('agriculture:conversation:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgriConversation agriConversation)
    {
        startPage();
        List<AgriConversation> list = agriConversationService.selectAgriConversationList(agriConversation);
        return getDataTable(list);
    }

    /**
     * 导出农业问答会话列表
     */
    @PreAuthorize("@ss.hasPermi('agriculture:conversation:export')")
    @Log(title = "农业问答会话", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AgriConversation agriConversation)
    {
        List<AgriConversation> list = agriConversationService.selectAgriConversationList(agriConversation);
        ExcelUtil<AgriConversation> util = new ExcelUtil<AgriConversation>(AgriConversation.class);
        util.exportExcel(response, list, "农业问答会话数据");
    }

    /**
     * 获取农业问答会话详细信息
     */
    @PreAuthorize("@ss.hasPermi('agriculture:conversation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(agriConversationService.selectAgriConversationById(id));
    }

    /**
     * 新增农业问答会话
     */
    @PreAuthorize("@ss.hasPermi('agriculture:conversation:add')")
    @Log(title = "农业问答会话", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgriConversation agriConversation)
    {
        // 设置默认状态为 open
        if (agriConversation.getStatus() == null) {
            agriConversation.setStatus("open");
        }

        int result = agriConversationService.insertAgriConversation(agriConversation);
        if (result > 0) {
            // 返回新创建的会话信息，包含自动生成的ID
            return AjaxResult.success("新增成功", agriConversation);
        } else {
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 修改农业问答会话
     */
    @PreAuthorize("@ss.hasPermi('agriculture:conversation:edit')")
    @Log(title = "农业问答会话", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AgriConversation agriConversation)
    {
        return toAjax(agriConversationService.updateAgriConversation(agriConversation));
    }

    /**
     * 删除农业问答会话
     */
    @PreAuthorize("@ss.hasPermi('agriculture:conversation:remove')")
    @Log(title = "农业问答会话", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(agriConversationService.deleteAgriConversationByIds(ids));
    }

    /**
     * 更新会话的专家回答者
     */
    @PreAuthorize("@ss.hasPermi('agriculture:conversation:edit')")
    @Log(title = "更新会话专家", businessType = BusinessType.UPDATE)
    @PutMapping("/updateExpert")
    public AjaxResult updateExpert(@RequestBody AgriConversation agriConversation)
    {
        // 只更新expertId和status
        AgriConversation updateConversation = new AgriConversation();
        updateConversation.setId(agriConversation.getId());
        updateConversation.setExpertId(agriConversation.getExpertId());
        updateConversation.setStatus("answered"); // 标记为已回答

        return toAjax(agriConversationService.updateAgriConversation(updateConversation));
    }
}
